import numpy as np
import os


def gen_l1loss_2d_data():
    #np.random.seed(42)
    os.makedirs("./input", exist_ok=True)
    os.makedirs("./output", exist_ok=True)

    # 生成二维数据 [batch_size, features]
    batch_size = 8
    features = 2048
    
    # 生成预测值 input (模型输出)--x
    input_pred = np.random.uniform(0, 1, [batch_size, features]).astype(np.float32)
    
    # 生成目标值 target (真实值)--y
    input_target = np.random.uniform(0, 1, [batch_size, features]).astype(np.float32)

    # 计算绝对差值
    abs_diff = np.abs(input_pred - input_target).astype(np.float32)

    # 计算三种 reduction 模式的损失
    loss_mean = np.mean(abs_diff).astype(np.float32)
    loss_sum = np.sum(abs_diff).astype(np.float32)

    # 生成全为0的s文件（128个uint16，全0）
    s_zeros = np.zeros(128, dtype=np.uint16)

    tiling = np.array([16384, 8], dtype=np.uint32)
    tiling.tofile("./input/input_tiling.bin")

    # 保存输入文件
    input_pred.tofile("./input/input_x.bin")
    input_target.tofile("./input/input_y.bin")
    s_zeros.tofile("./input/input_s.bin")  # 保存全0的s文件

    # 保存输出文件（三种 reduction 模式）
    np.array([loss_mean], dtype=np.float32).tofile("./output/golden_mean.bin")
    np.array([loss_sum], dtype=np.float32).tofile("./output/golden_sum.bin")
    abs_diff.tofile("./output/golden_none.bin")

    # 输出前16个值
    print("=== 前16个值 ===")
    print("input_x (前16个):", input_pred.flatten()[:16])
    print("input_y (前16个):", input_target.flatten()[:16])
    print("input_s (前16个):", s_zeros[:16])
    print("\n=== 统计信息 ===")
    print("input_x shape:", input_pred.shape)
    print("input_y shape:", input_target.shape)
    print("input_s shape:", s_zeros.shape)
    print("abs_diff shape:", abs_diff.shape)
    print("loss_mean:", loss_mean)
    print("loss_sum:", loss_sum)


if __name__ == "__main__":
    gen_l1loss_2d_data()